package mn.idax.exchange.otc.interceptor;

import mn.idax.exchange.auth.interceptor.CheckTokenInterceptor;
import mn.idax.exchange.auth.util.AuthInfoUtils;
import mn.idax.exchange.otc.annotation.LoginAuth;
import mn.idax.exchange.otc.entity.OTCUser;
import mn.idax.exchange.otc.service.OTCUserService;
import mn.idax.exchange.otc.service.UserService;
import mn.idax.exchange.otc.util.DateUtils;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.util.UUidUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Auther: dison
 * @Date: 2018/8/22 11:57
 * @Description:
 */
public class LoginAuthInterceptor extends CheckTokenInterceptor {

    private OTCUserService otcUserService;
    private UserService userService;

    public void setOtcUserService(OTCUserService otcUserService) {
        this.otcUserService = otcUserService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)){
            //获得注解对象
            LoginAuth loginAuth = ((HandlerMethod) handler).getMethodAnnotation(LoginAuth.class);
            if (loginAuth != null && loginAuth.disable()){
                return true;
            }
            return this.handler(httpServletRequest, httpServletResponse, handler);
        }
        return this.handler(httpServletRequest, httpServletResponse, handler);
    }

    public boolean handler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception{
        boolean handleFlag = super.preHandle(httpServletRequest, httpServletResponse, handler);
        if (handleFlag){
            return this.copyUser();
//            return true;
        }
        return false;
    }

    /**
     *
     * 功能描述:
     *      补齐用户信息
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/24 13:15
     */
    public boolean copyUser(){
        int loginUserId = AuthInfoUtils.getCurrent().getId();
//        if (loginConcurrentHashMap.contains(loginUserId)){
//            return;
//        }

        // 1.查询场内用户是否在场外用户表中存在
        OTCUser otcUser = this.otcUserService.getOTCUserByUserId(loginUserId);
        if (otcUser == null){
            Date currUTCDate = DateUtils.getUTCNow();
            // 2.补齐用户数据
            otcUser = new OTCUser();
            otcUser.setUserid(loginUserId);
            otcUser.setCreatetime(currUTCDate);
            otcUser.setUpdatetime(currUTCDate);
            otcUser.setUniquekey(UUidUtils.getUUID());
            try {
                return this.otcUserService.insertSelective(otcUser);
            } catch (Exception e) {
                LogUtils.error(this.getClass(), ExceptionUtils.getStackTrace(e)+"同步otc用户出错");
            }

//            // 3.修改用户昵称
//            User user = this.userService.getUserById(loginUserId);
//            if (user != null && StringUtils.isEmpty(user.getNickname())){
//                this.userService.updateNickName(user,user.getEmail());
//            }
        }
//        loginConcurrentHashMap.put(loginUserId,loginUserId);
        return true;
    }
}
